package com.yuexun.beilunpatient.ui.family.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.family.model.impl.FamilyModel;
import com.yuexun.beilunpatient.ui.family.presenter.impl.FamilyModifyPresenter;
import com.yuexun.beilunpatient.ui.family.ui.view.IFamilyModifyView;
import com.yuexun.beilunpatient.ui.main.ui.Act_Webview;
import com.yuexun.beilunpatient.widget.ActionSheetDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/26.
 */
public class Act_Family_Add extends BaseKJActivity implements IFamilyModifyView {

    @Bind(R.id.tv_username)
    EditText tvUsername;
    @Bind(R.id.treatmentCard)
    EditText treatmentCard;
    @Bind(R.id.tv_identity)
    EditText tvIdentity;
    @Bind(R.id.tv_relation)
    TextView tvRelation;
    @Bind(R.id.tv_phone)
    EditText tvPhone;
    @Bind(R.id.username_card)
    EditText usernameCard;
    @Bind(R.id.tv_address)
    EditText tvAddress;

    FamilyModifyPresenter presenter;
    int relation = 0;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_family_add);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new FamilyModifyPresenter(new FamilyModel(), this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.tv_right, R.id.ll_relation, R.id.cardHelp})
    public void onViewClicked(View view) {
        ActionSheetDialog actionSheetDialog;
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.tv_right:
                if (tvUsername.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入姓名");
                    break;
                }
                if (tvIdentity.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入身份证号");
                    break;
                }
                if (tvRelation.getText().toString().isEmpty()) {
                    ViewInject.toast("请选择亲属关系");
                    break;
                }
                if(tvPhone.getText().toString().isEmpty()){
                    ViewInject.toast("请输入手机号");
                    break;
                }
                if(usernameCard.getText().toString().isEmpty()){
                    ViewInject.toast("请输入就诊卡号");
                    break;
                }
                Map<String, String> params = new HashMap<>();
                params.put("memberNum", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.MEMBENUM));
                params.put("accessToken", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCESSTOKEN));
                params.put("telephone", tvPhone.getText().toString());
                params.put("address", tvAddress.getText().toString());
                params.put("treatmentCard", usernameCard.getText().toString());
                params.put("idCard", tvIdentity.getText().toString());
                params.put("name", tvUsername.getText().toString());
                params.put("relation", relation + "");
                params.put("sex", "1");
                params.put("patientId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.PATIENT_ID));
                params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                presenter.modifyAppoinmentFamilyNum(params);
                break;
            case R.id.cardHelp:
                Intent intentHelp = new Intent(aty, Act_Webview.class);
                intentHelp.putExtra("url", "https://gzjk.nbwjw.gov.cn/f/txbz");
                intentHelp.putExtra("title", "填写帮助");
                startActivity(intentHelp);
                break;
            case R.id.ll_relation:
                actionSheetDialog = new ActionSheetDialog(this).builder()
                        .setCancelable(true)
                        .setCanceledOnTouchOutside(true)
                        .addSheetItem("父母", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvRelation.setText("父母");
                                        relation = 1;
                                    }
                                })
                        .addSheetItem("子女", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvRelation.setText("子女");
                                        relation = 2;
                                    }
                                })
                        .addSheetItem("其他", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        tvRelation.setText("其他");
                                        relation = 3;
                                    }
                                });
                actionSheetDialog.show();
                break;
        }
    }

    @Override
    public void modifyAppoinmentFamilyNum(BaseEntity<String> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            finish();
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }

}
